<template>
  <el-dialog
    title="审核"
    :visible.sync="visible"
    width="500px"
  >
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item prop="status" label="结果" :rules="[{required: true, message: ' '}]" >
        <el-radio-group v-model="form.status">
          <el-radio :label="4">结束任务</el-radio>
          <el-radio :label="5">不通过</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="reviewNotes" label="备注">
        <el-input type="textarea" :rows="2" v-model="form.reviewNotes"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button type="primary" @click="onSubmit">确定</el-button>
      <el-button type="default" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>


<script>
import _ from 'underscore';

export default {
  data() {
    return {
      visible: false,
      form: {
        reviewNotes: '',
        status: null
      }
    }
  },
  methods: {
    show(orders, onSuccess) {
      this.visible = true;
      this.orders = orders;
      this.onSuccess = onSuccess;
      this.form = {
        reviewNotes: '',
        status: null,
        orderCodes: _.pluck(orders, 'orderno').join(',')
      };
    },
    async onSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return;
        if (this.form.status == 4) {
          for (let order of this.orders) {
            if (order.status == 1 || order.status == 3) {
              this.$message.warning(`${order.orderno}的状态为未完成, 不能选择结束`);
              return;
            }
          }
        }
        const ret = await this.$http.post('/api/volunteer/order/review', this.form);
        if (ret.success) {
          this.$message.success('审核成功');
          this.visible = false;
          this.orders.forEach(order => {
            order.status = this.form.status;
          });
          this.onSuccess();
        }
      });
    }
  },
  canReview(order) {
    return [1, 2].includes(order.status);
  }
}
</script>